<?php
class PNItemArray extends PNObjectArray {
  function PNItemArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'ishopstore_item';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';


    $this->_init($init, $where);
  }

  function genSort(){
    $sort  = FormUtil::getPassedValue ('order', false);
    $result = false;
    
    switch ($sort){
      case 'item_ref_id':
        $result = 'ORDER BY itm_item_ref_id ASC';
        break;
      case 'categories':
        $result = 'ORDER BY itm_categories_id';
        break;
      case 'subcategories':
        $result = 'ORDER BY itm_subcategories_id';
        break;
      case 'title':
        $result = 'ORDER BY itm_title';
        break;
      case 'amount':
        $result = 'ORDER BY itm_amount';
        break;      
      case 'date':
        $result = 'ORDER BY itm_cr_date DESC';
        break;
    }
    if ($result){
      return $result;
    }else{
      //default order
      return 'ORDER BY itm_cr_date';
    }
  
  }

  function genFilter(){
    $filter  = FormUtil::getPassedValue ('filter', false);
    $keyword = DataUtil::formatForStore(FormUtil::getPassedValue ('keyword', $filter['keyword']));
    $type = DataUtil::formatForStore(FormUtil::getPassedValue ('type', $filter['type']));
    $form = FormUtil::getPassedValue ('form', false );
    $categories_id_form = $form[categories_id];
    $categories_id = FormUtil::getPassedValue ('categories_id', false);
    //var_dump($categories_id);exit;
    //echo $categories_id; //exit;
    $wheres = array();
    //filter by keyword
    if ($keyword){
         $wheres[] = "itm_title LIKE '%". $keyword ."%'";
    }
    //filter by selector
    if ($categories_id && intval($categories_id)) $wheres[] = "itm_categories_id ='$categories_id'";

/*    //not display admin
    $pagetype = InnoUtil::getPageType();
    $admin_id = constant('ADMIN_UID');
    if ($pagetype == 'home' && $user_id == $admin_id){
      $wheres[] = "(itm_cr_uid <> '$admin_id' OR itm_obj_status = '1')";
    }
*/
    //filter by user_id
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    if ($user_id) $wheres[] = "itm_cr_uid = '$user_id'";

    return implode(' OR ', $wheres);
  }
  

}
